//
//  VisitorView.m
//  OCweibo
//
//  Created by 郑兆远 on 15/12/21.
//  Copyright © 2015年 zzy. All rights reserved.
//

#import "VisitorView.h"
#import "Masonry.h"
#import "UILabel+UILabel_EXtension.h"
#import "UIButton+UIButton_EXtension.h"

@interface VisitorView ()
@property(nonatomic, strong) UIImageView *backView;
@property(nonatomic, strong) UIImageView *iconView;
@property(nonatomic, strong) UIButton *loginBtn;
@property(nonatomic, strong) UIButton *registerBtn;
@property(nonatomic, strong) UIImageView *circleView;
@property(nonatomic, strong) UILabel *tipLabel;
@end
@implementation VisitorView
//直到view的frame所以不调用initwithframe
- (instancetype)init {
  if (self = [super init]) {
    [self setupUI];
    [self loadSubview];
    [self startAnimation];
  }
  return self;
}
- (void)loadSubview {
  //一定要注意添加的顺序
  [self addSubview:_circleView];
  [self addSubview:_backView];
  [self addSubview:_iconView];
  [self addSubview:_tipLabel];
  [self addSubview:_loginBtn];
  [self addSubview:_registerBtn];
  for (UIView *item in self.subviews) {
    //关闭自动布局
    item.translatesAutoresizingMaskIntoConstraints = NO;
  }
}

- (void)setupIconImage:(NSString *)imageName tipText:(NSString *)text {
  if (imageName) {
    self.iconView.hidden = YES;
    self.circleView.image = [UIImage imageNamed:imageName];
    self.tipLabel.text = text;
    [self bringSubviewToFront:self.circleView];
  } else {
    [self startAnimation];
  }
}
- (void)startAnimation {
  CABasicAnimation *anim =
      [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
  anim.repeatCount = MAXFLOAT;
  anim.duration = 20;
  anim.toValue = [NSValue
      valueWithCGAffineTransform:(CGAffineTransformMakeRotation(2 * M_PI))];
  anim.removedOnCompletion = NO;
  [self.circleView.layer addAnimation:anim forKey:nil];
}
- (void)layoutSubviews {
  [super layoutSubviews];
  self.backgroundColor = [UIColor colorWithWhite:0.93 alpha:1];
  //给circleView加动画

  [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.equalTo(self.mas_centerX);
    make.centerY.equalTo(self.mas_centerY).with.offset(-60);
  }];
  [self.circleView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.centerY.equalTo(self.iconView);
  }];
  [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.centerY.equalTo(self.iconView);
    make.left.equalTo(self.mas_left);
    make.right.equalTo(self.mas_right);
  }];
  [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.equalTo(self.backView.mas_centerX);
    make.top.equalTo(self.backView.mas_bottom).with.offset(16);
    make.height.mas_equalTo(40);
    make.width.mas_equalTo(230);
  }];
  [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.tipLabel.mas_left);
    make.top.equalTo(self.tipLabel.mas_bottom).with.offset(20);
    make.height.mas_equalTo(35);
    make.width.mas_equalTo(100);
  }];
  [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.tipLabel.mas_right);
    make.top.equalTo(self.tipLabel.mas_bottom).with.offset(20);
    make.height.mas_equalTo(35);
    make.width.mas_equalTo(100);
    make.left.equalTo(self.loginBtn.mas_right).with.offset(30);
  }];
}
- (void)setupUI {
  _backView = [[UIImageView alloc]
      initWithImage:[UIImage
                        imageNamed:@"visitordiscover_feed_mask_smallicon"]];
  _iconView = [[UIImageView alloc]
      initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
  _circleView = [[UIImageView alloc]
      initWithImage:[UIImage
                        imageNamed:@"visitordiscover_feed_image_smallicon"]];
  _tipLabel =
      [UILabel labelWithTitle:@"关注一些人,回这里看看有什么惊喜"
                        color:[UIColor darkGrayColor]
                     fontSize:14
                       margin:0];
  _loginBtn = [UIButton buttonWithTitle:@"登录"
                                  color:themeColor
                                   font:15
                          backImageName:@"common_button_white_disable"];
  _registerBtn = [UIButton buttonWithTitle:@"注册"
                                     color:themeColor
                                      font:15
                             backImageName:@"common_button_white_disable"];
}
@end
